﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using Buca.Application.aBigTime.WindowsForm.FormBase.PropertyGrid;
using Buca.Application.aBigTime.WindowsForm.Resources;
using DevExpress.XtraEditors;

namespace Buca.Application.aBigTime.WindowsForm.FormBase
{
    public partial class FrmBaseTreeDetail : Form
    {
        #region Variables

        /// <summary>
        /// 
        /// </summary>
        public enum ActionMode
        {
            AddNew = 0,
            Edit = 1,
            Delete = 2,
            None = 3
        }
        /// <summary>
        /// Gets or sets the e action mode.
        /// </summary>
        /// <value>
        /// The e action mode.
        /// </value>
        public ActionMode eActionMode;
        /// <summary>
        /// The key for send
        /// </summary>
        private string KeyForSend;
        /// <summary>
        /// The has children
        /// </summary>
        public new bool HasChildren;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the form caption.
        /// </summary>
        /// <value>
        /// The form caption.
        /// </value>
        [Category("BaseProperty")]
        public string FormCaption { get; set; }
        /// <summary>
        /// Gets or sets the key value.
        /// </summary>
        /// <value>
        /// The key value.
        /// </value>
        [Category("BaseProperty")]
        public string KeyValue { get; set; }

        #endregion

        #region Functions

        /// <summary>
        /// Initializes the layout.
        /// </summary>
        private void InitializeLayout()
        {
            Text = eActionMode == ActionMode.AddNew ? "Thêm mới " + FormCaption : "Sửa " + FormCaption;
            InitControls();
        }
        /// <summary>
        /// Closes the form.
        /// </summary>
        private void CloseForm()
        {
            using (FrmBaseCategoryList frmBaseList = new FrmBaseCategoryList())
            {
                if (PostKeyValue != null) PostKeyValue(this, KeyForSend);
                Close();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="keyValue">The key value.</param>
        public delegate void EventPostKeyHandler(object sender, string keyValue);
        /// <summary>
        /// The key value
        /// </summary>
        public event EventPostKeyHandler PostKeyValue;

        #endregion

        #region Functions overrides

        /// <summary>
        /// Shows the help.
        /// </summary>
        protected virtual void ShowHelp()
        {
        }
        /// <summary>
        /// Valids the data.
        /// </summary>
        /// <returns></returns>
        protected virtual bool ValidData()
        {
            return true;
        }
        /// <summary>
        /// Saves the data.
        /// </summary>
        /// <returns></returns>
        protected virtual int SaveData()
        {
            return 0;
        }
        /// <summary>
        /// Initializes the data.
        /// </summary>
        protected virtual void InitData()
        {
        }
        /// <summary>
        /// Focuses the control.
        /// </summary>
        protected virtual void InitControls()
        {
        }
        /// <summary>
        /// Automatics the reference no.
        /// </summary>
        protected virtual void AutoRefNo()
        {
        }

        #endregion

        #region Events

        /// <summary>
        /// Initializes a new instance of the <see cref="FrmBaseTreeDetail"/> class.
        /// </summary>
        public FrmBaseTreeDetail()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Handles the Load event of the FrmBaseTreeDetail control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void FrmBaseTreeDetail_Load(object sender, EventArgs e)
        {
            try
            {
                InitializeLayout();
                InitData();
                if (eActionMode == ActionMode.AddNew) AutoRefNo();
            }
            catch (Exception ex) { XtraMessageBox.Show(ex.Message, ResourceHelper.GetResourceValueByName("ResExceptionCaption"), MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (ValidData() == true)
                {
                    if (XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResSaveData"),
                                                ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK)
                    {
                        var rowAffected = SaveData();
                        if (rowAffected > 0)
                        {
                            XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResSaveDataSuccess"), ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                            KeyForSend = eActionMode == ActionMode.AddNew ? rowAffected.ToString() : KeyValue;
                            CloseForm();
                        }
                        else XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResSaveDataError"), ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch (Exception ex) { XtraMessageBox.Show(ex.Message, ResourceHelper.GetResourceValueByName("ResExceptionCaption"), MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }
        /// <summary>
        /// Handles the Click event of the btnExit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }
        #endregion
    }
}